#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <pullsdk.h>
#include <commapi.h>
#include "bluetooth_interface.h"


extern JNIEnv * g_env;
extern struct gBTCommClassInfo gBluetoothCommuClassInfo;

int BTInit(const char *BTAddress)
{
#if 0
	JNIEnv * env = g_env;
	int ret = 0;
	jstring jstrMSG = NULL;
	jstrMSG = env->NewStringUTF(BTAddress);
	ret = env->CallStaticIntMethod(gBluetoothCommuClassInfo.clazz, gBluetoothCommuClassInfo.BluetoothInit, jstrMSG);

	env->DeleteLocalRef(jstrMSG);

	UDK_LOGD( "BluetoothInit ret=%d", ret);

	return ret;
#endif
    return 0;
}

int BTFree(void)
{
#if 0
	JNIEnv * env = g_env;
	return env->CallStaticIntMethod(gBluetoothCommuClassInfo.clazz, gBluetoothCommuClassInfo.BluetoothFree);
#endif
    return 0;
}

int BTWrite(char *buf, int offset, int count)
{
	JNIEnv * env = g_env;
	jbyteArray jarrRV =env->NewByteArray(count);
	env->SetByteArrayRegion(jarrRV, 0,count, (jbyte *)buf);//copy c++ array to jbytearray
	int ret = env->CallIntMethod(gBluetoothCommuClassInfo.object, gBluetoothCommuClassInfo.BluetoothWrite, jarrRV, (jint)offset, (jint)count);
	env->DeleteLocalRef(jarrRV);
	return ret;
}

int BTRead(char *buf, int offset, int count)
{
	JNIEnv * env = g_env;
	jbyteArray jarrRV =env->NewByteArray(count);
	int ret = env->CallIntMethod(gBluetoothCommuClassInfo.object, gBluetoothCommuClassInfo.BluetoothRead, jarrRV, (jint)offset, (jint)count);
    if (ret > 0) {
        env->GetByteArrayRegion(jarrRV, 0, ret, (jbyte *)buf);//copy jbytearray to c++ array
    }
	env->DeleteLocalRef(jarrRV);
	return ret;
}

int BTClear()
{
    return 0;
}

int BTSelect(void)
{
#if 0
	JNIEnv * env = g_env;
	int len = env->CallStaticIntMethod(gBluetoothCommuClassInfo.clazz, gBluetoothCommuClassInfo.BluetoothSelect);

	return len;
#endif
    return 0;
}
